package main

import (
	"fmt"
)

// 自己实现的链表
/****************************************/
/**
head := NewListNode([]int{1, 2, 3})
printLinkedList(head)
*/

type ListNode struct {
	Val  int
	Next *ListNode
}

func NewListNode(nums []int) *ListNode {
	return createLinkedList(nums)
}

func createLinkedList(nums []int) *ListNode {

	if len(nums) == 0 { return nil }

	head := &ListNode{ Val: nums[0] }

	cur := head
	for i := 1; i < len(nums); i++ {
		cur.Next = &ListNode{ Val: nums[i] }
		cur = cur.Next
	}

	return head
}

func printLinkedList(head *ListNode) {

	cur := head
	for cur != nil {
		fmt.Print(cur.Val, "->")
		cur = cur.Next
	}
	fmt.Println("NULL")
}
/****************************************/

/**
执行用时：4 ms, 在所有 Go 提交中击败了76.16%的用户
内存消耗：3 MB, 在所有 Go 提交中击败了65.98%的用户
 */
func deleteDuplicates(head *ListNode) *ListNode {

	if head == nil || head.Next == nil { return head }

	dummy := &ListNode{Val: -1}
	dummy.Next = head

	a, b, c := dummy, dummy.Next, dummy.Next.Next
	for c != nil {
		for c != nil && b.Val == c.Val { c = c.Next }

		if b.Next != c {
			a.Next = c
		} else {
			a = b
		}
		b = c
		if c != nil { c = c.Next }
	}
	return dummy.Next
}

func main() {

	head := NewListNode([]int{1, 2, 3, 3, 4, 4, 5})
	printLinkedList(deleteDuplicates(head))
}
